/**
 * Created by smp on 2017/5/17.
 */
var isSearchBarExpand = false;
var index;

var modelPath=ROOT_PATH+'/archives/personArchives/securityMeetingLedger/';
var MEETING_TYPE=CONSTANT.DictionaryTypeDefConstants.MEETING_TYPE;

/**
 * 打开详情页面
 */
function openDetailPage(id){
    WindowManager.openLayer({
        type: 2,
        title: '详细信息',
        area: ['1200px', '90%'],
        content: modelPath + 'detail.shtml?id='+id
    });
}
$(function () {
    $('#deptId').combotree({
        url: ROOT_PATH+'/permission/department/getDepartmentTree.shtml?parentId=0'
    });

    $('#meetingType').combobox({
        url:ROOT_PATH+'/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code='+MEETING_TYPE,
        editable:false,
        valueField:'id',
        textField:'text'
    });
    $('#dg1').datagrid({
        method: 'post',
        url: modelPath + 'list.shtml',
        queryParams: {
            sort: 'addTime',
            order: 'desc'
        },
        // idField: 'id',
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect:true,
        sortOrder: true,
        striped: true,
        fit: true,
        columns: [[
            {field: 'id', width: 80, checkbox: true},
            {field: 'meetingName', title: '会议名称', width: 100,
                formatter:function(value,row,index){
                    return "<a href='javascript:void(0);' style='color: deepskyblue;' onclick='openDetailPage(\""+row.id+"\")'>"+value+"</a>"
                }, sortable: true},
            {field: 'conveneUnit', title: '召集单位', width: 80,  sortable: true},
            {field: 'meetingTypeName', title: '会议类型', width: 80},
            {field: 'meetingTime', title: '会议时间', width: 60},
            {field: 'meetingContent', title: '会议内容', width: 60, sortable: true},
            {field: 'addUserName', title: '登记人', width: 60},
            {field: 'addDeptName', title: '登记部门', width: 60},
            {field: 'addTime', title: '登记日期', width: 60, sortable: true}
        ]],
        toolbar: '#toolbar'
    });
    //添加按钮事件
    $('#securityMeetingLedgerAdd').on('click',function () {
        WindowManager.openLayer({
            type: 2,
            title: '添加会议',
            area: ['1200px', '90%'],
            content: modelPath + 'add.shtml',
            callBack:function () {
                $('#dg1').datagrid('reload');
            }
        });
    });
    //编辑按钮事件
    $('#securityMeetingLedgerEdit').on('click',function () {

        var checkedRows=$('#dg1').datagrid('getChecked');

        if(checkedRows.length == 0){
            layer.alert("选择需要编辑的行！");
            return
        }else if (checkedRows.length > 1){
            layer.alert("只能选中一行数据！");
            return
        }
        var id=checkedRows[0].id;
        WindowManager.openLayer({
            type: 2,
            title: '编辑组织',
            area: ['1200px', '90%'],
            content: modelPath + 'edit.shtml?id='+id,
            callBack:function () {
                $('#dg1').datagrid('reload');
            }
        });
    });
    //删除按钮事件
    $('#securityMeetingLedgerDelete').on('click',function () {

        var checkedRows=$('#dg1').datagrid('getChecked');

        if(checkedRows.length == 0){
            layer.alert("选择需要删除的行！");
            return
        }else if (checkedRows.length > 1){
            layer.alert("只能选中一行数据！");
            return
        }
        var id=checkedRows[0].id;
        layer.confirm('是否确定删除当前选中数据？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                type: "POST",
                url: modelPath+'delete.shtml',
                data: {id:id},
                dataType: "json",
                success: function(result){
                    if (result.success) {
                        $('#dg1').datagrid('clearSelections').datagrid('reload');
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 1000
                        });
                    }
                    else {
                        layer.msg('删除失败', {
                            icon: 2,
                            time: 1000
                        });
                    }
                }
            });
        });

    });

    var getSelectedUser2 = function (data) {
        var id=data[0].id;
        var realName=data[0].realName;
        $("#addUserId").val(id);
        $("#addUserName").textbox("setValue",realName);

    };

    $('#addUserName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                WindowManager.openLayer({
                    type: 2,
                    title: '添加承包人',
                    area: ['90%', '70%'],
                    content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
                    callBack:getSelectedUser2
                });
            }
        }],
        editable: false
    });
    /**
     * 高级查询列表
     */
    $('#btn_advanced_search').on("click", function () {
        $('#tb2').toggle();
        $('#dg1').datagrid('resize');
    });
    /**
     * 查询按钮
     */
    $('#selectMeeting').on('click',function(){
        var deptId=$('#deptId').textbox("getValue");
        var meetingName=$('#meetingName').textbox("getValue");
        var meetingContent=$('#meetingContent').textbox("getValue");
        var meetingBeginTime=$('#meetingBeginTime').datebox("getValue");
        var meetingEndTime=$("#meetingEndTime").datebox("getValue");
        var beginTime=$('#beginTime').datebox("getValue");
        var endTime=$('#endTime').datebox("getValue");
        var addUserId=$("#addUserId").val();
        var meetingType=$('#meetingType').textbox("getValue");
        $('#dg1').datagrid('load', {meetingContent:meetingContent,meetingBeginTime:meetingBeginTime,meetingEndTime:meetingEndTime,meetingType:meetingType,meetingName:meetingName,deptId:deptId,beginTime:beginTime,endTime:endTime,addUserId:addUserId,sort: 'addTime', order: 'desc'});
    });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
        $('#tb2').form('reset');
    });
});